package com.ailk.tcm.hffw.android.common.network;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ResponseObject {
	private boolean success;
	private String message;
	private String sourceJsonString;
	private JSONObject sourceJsonObject;
	private JSONObject jsonObjectData;
	private Object objectData;
	private List<?> arrayData;
	
	public ResponseObject() {}
	
	public ResponseObject(String sourceJsonString) {
		this.sourceJsonString = sourceJsonString;
		sourceJsonObject = JSON.parseObject(sourceJsonString);
		success = sourceJsonObject.getBooleanValue("success");
		message = sourceJsonObject.getString("message");
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getSourceJsonString() {
		return sourceJsonString;
	}
	
	public JSONObject getJsonObjectData() {
		if(sourceJsonObject != null){
			if(jsonObjectData == null){
				jsonObjectData = sourceJsonObject.getJSONObject("data");
			}
		}
		return jsonObjectData;
	}

	@SuppressWarnings("unchecked")
	public <T> T getObjectData(Class<T> clazz) {
		if(sourceJsonObject != null){
			if(objectData == null){
				objectData = JSON.parseObject(sourceJsonObject.getString("data"), clazz);
			}
			return (T)objectData;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public <T> List<T> getArrayData(Class<T> itemClazz) {
		if(sourceJsonObject != null){
			if(arrayData == null){
				arrayData = JSON.parseArray(sourceJsonObject.getString("data"), itemClazz);
			}
			return (List<T>)arrayData;
		}
		return null;
	}
}
